﻿using Algorithm.Check;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static Algorithm.Check.ExtensionForCRC16;
using static OfficeOpenXml.ExcelErrorValue;

namespace NewDevice.RS485
{
    /// <summary>
    /// Yeyy伺服/步进电机控制器
    /// </summary>
    public class YeyyMotionControl
    {
        private int _slaveId;
        private SerialBasePlus SerialBasePlus { get; }

        public YeyyMotionControl(int SlaveId, string portName, int baudRate = 9600)
        {
            SerialBasePlus=new SerialBasePlus(SlaveId, portName, baudRate);
            _slaveId=SlaveId;
            SerialBasePlus.Open();
        }

        //电机正转启动
        public void LeftRun()
        {
            byte[] data = [
                (byte)_slaveId,
                0x06,
                0x00,
                0x18,
                0x00,
                0x01
            ];

            byte[] cmd = data.WithCRC16(CRC16Type.Modbus).ToArray();

            var res = SerialBasePlus.SendRepeat(cmd);
        }

        //电机正转停止
        public void LeftStop()
        {
            byte[] data = [
                (byte)_slaveId,
                0x06,
                0x00,
                0x18,
                0x00,
                0x00
            ];

            byte[] cmd = data.WithCRC16(CRC16Type.Modbus).ToArray();

            var res = SerialBasePlus.SendRepeat(cmd);
        }

        //电机反转启动
        public void RightRun()
        {
            byte[] data = [
                (byte)_slaveId,
                0x06,
                0x00,
                0x17,
                0x00,
                0x01
            ];

            byte[] cmd = data.WithCRC16(CRC16Type.Modbus).ToArray();

            var res = SerialBasePlus.SendRepeat(cmd);
        }

        //电机反转停止
        public void RightStop()
        {
            byte[] data = [
                (byte)_slaveId,
                0x06,
                0x00,
                0x17,
                0x00,
                0x00
            ];

            byte[] cmd = data.WithCRC16(CRC16Type.Modbus).ToArray();

            var res = SerialBasePlus.SendRepeat(cmd);
        }

        /// <summary>
        /// 读取X1转限位标志
        /// </summary>
        public bool ReadX1Sign()
        {
            byte[] data = [
                (byte)_slaveId,
                0x03,
                0x00,
                0x22,
                0x00,
                0x02

            ];

            byte[] cmd = data.WithCRC16(CRC16Type.Modbus).ToArray();

            var res = SerialBasePlus.SendRepeat(cmd);
            return res[4]==1 ? true : false;
        }

        /// <summary>
        /// 读取X2转限位标志
        /// </summary>
        public bool ReadX2Sign()
        {
            byte[] data = [
                (byte)_slaveId,
                0x03,
                0x00,
                0x23,
                0x00,
                0x02

            ];

            byte[] cmd = data.WithCRC16(CRC16Type.Modbus).ToArray();

            var res = SerialBasePlus.SendRepeat(cmd);
            return res[4]==1 ? true : false;
        }

        public void Close()
        {
            SerialBasePlus.Dispose();
        }
    }
}